﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Serialization;
using Startup.Project.Algorithm;
using Startup.Project.Classes;
using Startup.Project.Interfaces;
using Startup.Shell.Domain;
using Startup.Shell.Domain.FunctionalityContracts;

namespace Startup.Shell.ModelDesignerPreview
{
	[XmlInclude(typeof(LoadCommandContract))]
	[XmlInclude(typeof(SaveCommandContract))]
	[XmlInclude(typeof(NewCommandContract))]
	[XmlInclude(typeof(AddCommandContract))]
	public class CommandsPanelPreview : IDesignerPreview
	{
		private readonly ObservableCollection<CommandContractBase> _commands = new ObservableCollection<CommandContractBase>();

		public override ImageSource PreviewImage
		{
			get { return new BitmapImage(new Uri("/Startup.Shell;component/Resources/RendererPanel.png", UriKind.RelativeOrAbsolute)); }
		}

		public override PreviewContainer[] Children
		{
			get { return new PreviewContainer[0]; }
		}

		public override IDesignerPreview CreateInstance()
		{
			return new CommandsPanelPreview();
		}

		public override UiContainer GetContainer(ClassBase dataContextType, StartupProperty property)
		{
			ToolbarContainer container = new ToolbarContainer(0);
			
			for (int index = 0; index < _commands.Count; index++)
			{
				CommandContractBase command = _commands[index];
				CommandDefinition commandDefinition = new CommandDefinition
				{
					DisplayName = command.CommandName,
					CommandName = string.Format("{0}Command", command.CommandName),
					CommandContent = command.GetContent(dataContextType, property),
					TargetProperty = property.Name
				};

				container.AddCommand(commandDefinition);
			}

			return container;
		}

		public ObservableCollection<CommandContractBase> Commands
		{
			get { return _commands; }
		}
	}
}
